home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 57670 / 57670.xpi / chrome / content / clpics.js < prev    next >
Text File  |  2010-02-06  |  12KB  |  351 lines

  1. var clpics = {}
  2. clpics.callbacks = {}
  3.  
  4. clpics.init = function (){
  5.     gBrowser.addEventListener("DOMContentLoaded", function (){
  6.         clpics.pref("enable")
  7.             ?clpics.run()
  8.             :null
  9.     }, false)
  10. }
  11. window.addEventListener("load", clpics.init, true)
  12.  
  13. clpics.locale = function (name, args){
  14.     var strings = document.getElementById("clpics-strings")
  15.     return (args != undefined)
  16.         ? strings.getFormattedString(name, args)
  17.         : strings.getString(name)
  18. }
  19.  
  20. clpics.pref = function (name, val){
  21.     name = "extensions.clpics."+name
  22.     return (val != undefined)
  23.         ? clpics.rawpref(name, val)
  24.         : clpics.rawpref(name)
  25. }
  26. clpics.rawpref = function (name, val){
  27.     var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch)
  28.     if (val != undefined)
  29.         switch(prefs.getPrefType(name)){
  30.             case prefs.PREF_STRING:
  31.                 return prefs.setCharPref(name, val)
  32.             case prefs.PREF_INT:
  33.                 return prefs.setIntPref(name, val)
  34.             case prefs.PREF_BOOL:
  35.                 return prefs.setBoolPref(name, val)
  36.         }
  37.     else
  38.         switch(prefs.getPrefType(name)){
  39.             case prefs.PREF_STRING:
  40.                 return prefs.getCharPref(name)
  41.             case prefs.PREF_INT:
  42.                 return prefs.getIntPref(name)
  43.             case prefs.PREF_BOOL:
  44.                 return prefs.getBoolPref(name)
  45.         }
  46. }
  47.  
  48. clpics.log = function (str){
  49.     var console = Components.classes["@mozilla.org/consoleservice;1"]
  50.                     .getService(Components.interfaces.nsIConsoleService)
  51.     console.logStringMessage("CLPics: "+str)
  52. }
  53. clpics.logobj = function (obj){
  54.     var t = []
  55.     for (var i in obj)
  56.         try {
  57.             t.push(i+": "+String(obj[i]).split("\n").reverse().pop())
  58.         }
  59.         catch(e){}
  60.     clpics.log(t.sort().join("\n"))
  61. }
  62.  
  63. clpics.get_new_image_dimensions = function (width, height){
  64.     var results = {
  65.                     width: width,
  66.                     height: height
  67.                 }
  68.     if (!clpics.pref("resize_images")) return results
  69.  
  70.     switch (clpics.pref("resize_method")) {
  71.         case "image_scale":
  72.             results.height = parseInt(height*clpics.pref("image_scale")/100.0)
  73.             results.width = parseInt(width*clpics.pref("image_scale")/100.0)
  74.             break
  75.         case "image_maxwidth":
  76.             results.width = Math.min(width, clpics.pref("image_maxwidth"))
  77.             results.height = parseInt(height*(results.width/width))
  78.             break
  79.         case "image_maxheight":
  80.             results.height = Math.min(height, clpics.pref("image_maxheight"))
  81.             results.width = parseInt(width*(results.height/height))
  82.             break
  83.     }
  84.     return results
  85. }
  86.  
  87. clpics.unloaded_images = {}
  88. clpics.div_count = 0
  89. clpics.do_p = function (p){
  90.     // Make sure we have content to fetch
  91.     var link = $("a", p)[0]
  92.     if (link == undefined) return
  93.     // Fetch page content, and send to main handling function.
  94.     // This allows for asynchronous handling
  95.     $.ajax({
  96.         type:  "GET",
  97.         url:   link.href,
  98.         async: true,
  99.         success: function (text){
  100.             // temp container to parse fetched HTML
  101.             var temp = content.document.createElement("div")
  102.             temp.innerHTML = text
  103.             clpics.mod_p(p, temp)
  104.         }
  105.     })
  106. }
  107. clpics.mod_p = function (p, temp){
  108.     var doc = content.document
  109.  
  110.     // If there's an h5, either 1) the posting is flagged, 2) the poster
  111.     //  knows how to use HTML and used h5s for some reason
  112.     if (temp.getElementsByTagName("h5").length > 0) {
  113.         var flagger = doc.createElement("small")
  114.         flagger.appendChild(doc.createTextNode(" "+clpics.locale("flagged")))
  115.         p.appendChild(flagger)
  116.         return
  117.     }
  118.  
  119.     if (clpics.pref("text_preview")) {
  120.         // userbody - user-typed content.  ie, "the ad"
  121.         var userbody = $("#userbody", temp)[0]
  122.  
  123.         // No userbody probably means a stray link/paragraph
  124.         if (userbody == undefined) return
  125.  
  126.         userbody = userbody.cloneNode(true)
  127.         $(".blurbs, .blurbs+table", userbody).each(function(){
  128.             userbody.removeChild(this)
  129.         })
  130.  
  131.         // extract user content
  132.         var text = userbody.textContent
  133.                             .replace(/" "{2,}/g, " ")
  134.                             .replace(/\n+/g, "\n")
  135.                             .replace(/^[\s\xA0]+|[\s\xA0]+$/g, "")
  136.                             .split("\n")
  137.  
  138.  
  139.         if (text.length > 0) {
  140.             // Text preview
  141.             var preview = doc.createElement("div")
  142.             preview.className = "clpics-preview"
  143.             if (clpics.pref("restrict_preview")) {
  144.                 preview.style.maxHeight = clpics.pref("preview_height")+"px"
  145.                 preview.style.overflow = "auto"
  146.                 preview.style.paddingRight = "10px"
  147.             }
  148.             for (var i=0; i<text.length; i++) {
  149.                 var newcont = doc.createElement("p")
  150.                 newcont.textContent = text[i]
  151.                 preview.appendChild(newcont)
  152.             }
  153.             p.appendChild(preview)
  154.  
  155.             // initial css
  156.             $(preview)
  157.                 .hide()
  158.                 .css("font-size", ".8em")
  159.                 .css("width", "60%")
  160.  
  161.             // show/hide image
  162.             var image = doc.createElement("img")
  163.             image.title = clpics.locale("togglepreview")
  164.             image.src = "chrome://clpics/skin/show-preview.png"
  165.             $(image).css("margin-right", ".5em")
  166.             p.insertBefore(image, p.firstChild)
  167.             $(image).click(function (){
  168.                 $(this).siblings(".clpics-preview").slideToggle('slow')
  169.             })
  170.         }
  171.     }
  172.  
  173.     if (clpics.pref("image_preview")) {
  174.         /* Begin Image Sanity Checks */
  175.         var span = $("span", p)
  176.         // If no <span>, skip
  177.         if (span.length == 0) return
  178.         span = span[0]
  179.         // If span doesn't contain "pic" or "img", skip
  180.         if (span.textContent.indexOf("pic") == -1 && span.textContent.indexOf("img") == -1) return
  181.         /* End Checks */
  182.  
  183.         // Set up image container
  184.         var cont = doc.createElement("div")
  185.         cont.style.position = "relative"
  186.         p.appendChild(cont)
  187.  
  188.         var images = $("img", temp)
  189.                         .each(function (){
  190.                             // prevent images from being loaded now
  191.                             this.id = this.src
  192.                             this.src = ""
  193.                             this.removeAttribute("src")
  194.  
  195.                             // detach from temp node
  196.                             this.parentNode.removeChild(this)
  197.                         })
  198.  
  199.         // Images
  200.         var limit = clpics.pref("image_limit")
  201.         var ix = 0
  202.         cont.id = "clpics-div-"+clpics.div_count++
  203.         clpics.unloaded_images[cont.id] = 0
  204.         images.each(function (){
  205.             // Use an a element to parse the image URL -- very handy
  206.             var parsed = doc.createElement("a")
  207.             parsed.href = this.id
  208.             if (clpics.pref("image_domain")) {
  209.                 if (
  210.                     !parsed.host.match("craigslist\.org$") &&
  211.                     !parsed.host.match("craigslist\.ca$")
  212.                 ) {
  213.                     if (!cont.imagesrc) cont.imagesrc = ""
  214.                     if (cont.imagesrc.indexOf(parsed.host) == -1) {
  215.                         cont.imagesrc += " "+parsed.host
  216.                     }
  217.                     return true
  218.                 }
  219.             }
  220.  
  221.             // if we've reached our limit, stop iteration
  222.             if (limit > 0 && ++ix > limit) return false
  223.  
  224.             // Move image to page
  225.             cont.appendChild(this)
  226.  
  227.             this.src = this.id
  228.  
  229.             $(this).css({
  230.                 display: "none",
  231.                 margin: "5px",
  232.                 border: "#000 solid 1px"
  233.             })
  234.             this.className = "clpics-image"
  235.             if (this.alt && this.alt != "") {
  236.                 this.id = "clpics-"+this.alt.split(" ")[0]
  237.                 this.removeAttribute("alt")
  238.             }
  239.             else
  240.                 this.removeAttribute("id")
  241.  
  242.             clpics.unloaded_images[cont.id]++
  243.             this.addEventListener("load", clpics.callbacks.resize_image, false)
  244.         })
  245.     }
  246. }
  247.  
  248. clpics.run = function (){
  249.     // Limit to craigslist pages
  250.     // process if we hav a document and (endswith .org OR endswith .ca)
  251.     if (
  252.             !content.document ||
  253.             (!content.document.location.host.match("craigslist\.org$") &&
  254.             !content.document.location.host.match("craigslist\.ca$"))
  255.         ) return
  256.     //if (!content.document.location.pathname == "/") return
  257.  
  258.     var doc = content.document
  259.  
  260.     // craigslist has kindly given us unique body classes to work with
  261.     //  hp - homepage
  262.     //  toc - listings, both search and category
  263.     //  posting - an ad
  264.     if (doc.body.className.indexOf("toc") != -1) {
  265.         $("blockquote p", doc).each(function (){
  266.             // Check if enabled first
  267.             if (!clpics.pref("enable")) return true
  268.  
  269.             // If p has flag class, skip
  270.             if ($(this).hasClass("clpics-modded")) return true
  271.  
  272.             // Set classname
  273.             $(this).addClass("clpics-modded")
  274.  
  275.             // Process, but only if the user wants us to
  276.             if (clpics.pref("text_preview") || clpics.pref("image_preview")) {
  277.                 clpics.do_p(this)
  278.             }
  279.         })
  280.     }
  281. }
  282.  
  283. clpics.callbacks.resize_image = function (){
  284.     $.fn.reverse = [].reverse
  285.     var new_dimensions = clpics.get_new_image_dimensions(
  286.             this.naturalWidth,
  287.             this.naturalHeight
  288.         )
  289.     this.height = new_dimensions.height
  290.     this.width = new_dimensions.width
  291.     this.style.display = "inline"
  292.  
  293.  
  294.     clpics.unloaded_images[this.parentNode.id]--
  295.     if (clpics.unloaded_images[this.parentNode.id] == 0 && clpics.pref("hover_zoom")) {
  296.         // re-position images
  297.         var doc = content.document
  298.  
  299.         $(this.parentNode).each(function (){
  300.             if (this.imagesrc &&
  301.                 this.imagesrc.replace(/^[\s\xA0]+|[\s\xA0]+$/g, "") != "") {
  302.                 var flagger = doc.createElement("small")
  303.                 flagger.appendChild(
  304.                             doc.createTextNode(
  305.                                 " "+clpics.locale("imageshosted",
  306.                                                     [this.imagesrc.replace(/^[\s\xA0]+|[\s\xA0]+$/g, "").split(" ").join(", ")]
  307.                                                 )
  308.                                             )
  309.                                 )
  310.                 if (this.childNodes.length > 0) {
  311.                     this.insertBefore(doc.createElement("br"), this.firstChild)
  312.                     this.insertBefore(flagger, this.firstChild)
  313.                 }
  314.                 else
  315.                     this.appendChild(flagger)
  316.             }
  317.             this.style.height = $(this).height()
  318.         })
  319.  
  320.         $("img", this.parentNode).reverse()
  321.         .each(function (){
  322.             $(this)
  323.                 .css("top", this.y-5)
  324.                 .css("left", this.x-5)
  325.                 .css("position", "absolute")
  326.  
  327.             this.proper_height = this.height
  328.             this.proper_width = this.width
  329.             this.proper_top = this.style.top
  330.         })
  331.         .mouseover(function (){
  332.             $(this)
  333.                 .css("z-index", 2)
  334.                 .animate({opacity: 1.0}, clpics.pref("hover_zoom_timeout"), function (){
  335.                     $(this).animate({
  336.                         height: this.naturalHeight,
  337.                         width: this.naturalWidth
  338.                     })
  339.                 })
  340.         })
  341.         .mouseout(function (){
  342.             $(this)
  343.                 .stop()
  344.                 .animate({
  345.                     height: this.proper_height,
  346.                     width: this.proper_width
  347.                 }, "normal", function (){$(this).css("z-index", 1)})
  348.         })
  349.     }
  350. }
  351.